package com.ganbo.ms.websocket.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;


@RestController
public class IndexController {
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
    private int i;

    @GetMapping("/index")
    public int index() throws Exception {
        cyclicBarrier.await();
        i++;
        return i;
    }
    public static void main(String[] args) {
        Executors.newFixedThreadPool(20);
        IndexController controller = new IndexController();
        for (int t = 0; t < 10; t++) {
            new Thread(() -> {
                int index = 0;
                try {
                    index = controller.index();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(index);
            }).start();
        }
    }
}
